package com.koron.css2.systemManage.service;

import com.github.pagehelper.PageInfo;
import com.koron.common.bean.query.OperationQueryBean;
import com.koron.common.bean.query.StaffQueryBean;
import com.koron.common.web.bean.StaffBean;
import com.koron.common.web.mapper.StaffMapper;
import com.koron.css2.ApplicationConfig;
import com.koron.css2.systemManage.bean.*;
import com.koron.css2.systemManage.mapper.OrgUserMapper;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.stream.Collectors;

public class InitService {
	
	/**
	 * 初始化用户的数据权限
	 */
	public MessageBean<?> initUserRange(SessionFactory factory) {
		OrgUserMapper orgUserMapper = factory.getMapper(OrgUserMapper.class);
		List<OrgUserBean> orgUserList = orgUserMapper.selectList(new OrgUserQueryBean());
		DataRangeService dataRangeService = new DataRangeService();
		NewDataRangeService newDataRangeService = new NewDataRangeService();
		for(OrgUserBean user : orgUserList) {
			List<UserRangeBean> userRangeList = dataRangeService.getUserRangeValueList(user.getAccount(), ApplicationConfig.getAppCustomerAppid(), "company");
			List<String> valueList = userRangeList.stream().map(t -> t.getValue()).collect(Collectors.toList());
			if(StringUtils.isNotBlank(user.getCode()) && valueList.size() > 0) {
				newDataRangeService.addUserRangeValue(ApplicationConfig.getAppCustomerAppid(), user.getCode(), "company", valueList);
			}
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "", Void.class);
	}
	
	/**
	 * 初始化菜单列表，将旧的权限平台的菜单列表同步到新的统一用户平台
	 */
	public MessageBean<?> initAllOperation() {
		OperationService operationService = new OperationService();
		NewOperationService newOperationService = new NewOperationService();
		OperationQueryBean query = new OperationQueryBean();
		query.setPage(1);
		query.setType(1);
		query.setPageCount(1024);
		query.setApp(ApplicationConfig.getAppCustomerAppid());
		PageInfo<OperationTreeBean> page = operationService.listAllOperation(query);
		List<OperationTreeBean> operationList = page.getList();
		for(OperationTreeBean operation : operationList) {
			newOperationService.addOperation(operation);
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "", Void.class);
	}
	
	/**
	 * 初始化角色菜单
	 */
	public MessageBean<?> initRoleOperation(SessionFactory factory) {
		RoleService roleService = new RoleService();
		OperationService operationService = new OperationService();
		NewOperationService newOperationService = new NewOperationService();
		List<RoleBean> roles = roleService.listRole(ApplicationConfig.getAppCustomerAppid());
		for(RoleBean role : roles) {
			List<OperationTreeBean> operationList = operationService.listOPByRole(ApplicationConfig.getAppCustomerAppid(), role.getRoleCode());
			List<String> opCodeList = operationList.stream().map(t -> t.getOpCode()).collect(Collectors.toList());
			newOperationService.addRoleOperation(role.getRoleCode(), opCodeList);
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "", Void.class);
	}
	
	
	/**
	 * 初始化用户角色
	 */
	public MessageBean<?> initUserRole(SessionFactory factory) {
		RoleService roleService = new RoleService();
		NewRoleService newRoleService = new NewRoleService();
		List<RoleBean> roles = roleService.listRole(ApplicationConfig.getAppCustomerAppid());
		OrgUserMapper orgUserMapper = factory.getMapper(OrgUserMapper.class);
		for(RoleBean role : roles) {
			PageInfo<OrgUserBean> page = roleService.listUserByRole(factory, role.getRoleCode(), 1, 1024);
			List<OrgUserBean> orgUsers = page.getList();
			List<String> users = orgUsers.stream().filter(t -> !StringUtils.isEmpty(t.getCode())).map(t -> t.getCode()).collect(Collectors.toList());
			if(users.size() > 0) {
				OrgUserQueryBean query = new OrgUserQueryBean();
				query.setUserAccounts(users);
				List<OrgUserBean> orgUser = orgUserMapper.selectByAccounts(query);
				newRoleService.addRoleUser(role.getRoleCode(), orgUser);
			}
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "", Void.class);
	}
	
	/**
	 * 初始化用户角色
	 */
	public MessageBean<?> initUserCode(SessionFactory factory) {
		StaffMapper staffMapper = factory.getMapper(StaffMapper.class, "_default");
		List<StaffBean> staffs = staffMapper.selectList(new StaffQueryBean());
		OrgUserMapper orgUserMapper = factory.getMapper(OrgUserMapper.class);
		for(StaffBean staff : staffs) {
			OrgUserBean orgUser = orgUserMapper.selectByAccount(staff.getLoginid());
			if(orgUser != null) {
				orgUser.setCode(staff.getCode());
				orgUserMapper.update(orgUser);
			}else {
				// orgUser
			}
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "", Void.class);
	}
}
